home *** CD-ROM | disk | FTP | other *** search
- // ===========================================================================
- // CBasicApp.cp ©1994-2000 Metrowerks Inc. All rights reserved.
- // ===========================================================================
- // This file contains the starter code for a basic PowerPlant project
-
- #include "CBasicApp.h"
-
- #include <LGrowZone.h>
- #include <PP_Messages.h>
- #include <PP_Resources.h>
- #include <UDrawingState.h>
- #include <UMemoryMgr.h>
- #include <URegistrar.h>
-
- #include <LWindow.h>
- #include <LCaption.h>
- #include <LPicture.h>
-
- #include <AERegistry.h>
-
- #include <SpeechSynthesis.h>
-
- // Constant declarations
- const ResIDT PPob_SampleWindow = 128;
-
- static int abs(int X)
- {
- return (X<0) ? -X : X;
- }
-
- // ===========================================================================
- // • main
- // ===========================================================================
-
- int main()
- {
- // Set Debugging options
- SetDebugThrow_(debugAction_Alert);
- SetDebugSignal_(debugAction_Alert);
-
- // Initialize Memory Manager. Parameter is the number of
- // master pointer blocks to allocate
- InitializeHeap(3);
-
- // Initialize standard Toolbox managers
- UQDGlobals::InitializeToolbox();
-
- // Install a GrowZone to catch low-memory situations
- new LGrowZone(20000);
-
- // Create the application object and run
- CBasicApp theApp;
- theApp.Run();
-
- return 0;
- }
-
-
- // ---------------------------------------------------------------------------
- // • CBasicApp [public]
- // ---------------------------------------------------------------------------
- // Application object constructor
-
- CBasicApp::CBasicApp()
- {
- unsigned long secs;
- RegisterClasses();
-
- theWindow = NULL;
- activeTime = 0;
-
- GetDateTime(&secs);
- SetQDGlobalsRandomSeed(secs);
-
- StartIdling();
- }
-
-
- // ---------------------------------------------------------------------------
- // • ~CBasicApp [public, virtual]
- // ---------------------------------------------------------------------------
- // Application object destructor
-
- CBasicApp::~CBasicApp()
- {
- // Nothing
- }
-
-
- // ---------------------------------------------------------------------------
- // • StartUp [protected, virtual]
- // ---------------------------------------------------------------------------
- // Perform an action in response to the Open Application AppleEvent.
- // Here, issue the New command to open a window.
-
- void
- CBasicApp::StartUp()
- {
- SetActivateTime();
- ShowMyWindow();
- }
-
- const int N_STRINGS = 6;
-
- void
- CBasicApp::GetSaying(StringPtr str)
- {
- GetIndString(str,128,(abs(Random())%N_STRINGS)+1);
- }
-
- void
- CBasicApp::ShowMyWindow()
- {
- if (theWindow == NULL)
- {
- Str255 str;
- ProcessSerialNumber psn;
-
- theWindow = LWindow::CreateWindow(PPob_SampleWindow, this);
- ThrowIfNil_(theWindow);
-
- GetSaying(str);
- theWindow->FindPaneByID(111)->SetDescriptor(str);
-
- GetCurrentProcess(&psn);
- SetFrontProcess(&psn);
-
- theWindow->Show();
-
- while (SpeechBusy()) {/* wait for any speech to complete */}
- SpeakString(str);
-
- }
- }
-
- void
- CBasicApp::DeleteMyWindow()
- {
- if (theWindow)
- delete theWindow;
- theWindow = NULL;
- }
-
- // ---------------------------------------------------------------------------
- // • ObeyCommand [public, virtual]
- // ---------------------------------------------------------------------------
- // Respond to Commands. Returns true if the Command was handled, false if not.
-
- Boolean
- CBasicApp::ObeyCommand(
- CommandT inCommand,
- void* ioParam)
- {
- Boolean cmdHandled = true; // Assume we'll handle the command
-
- switch (inCommand) {
-
- case cmd_New: {
- break;
- }
-
- default: {
- cmdHandled = LApplication::ObeyCommand(inCommand, ioParam);
- break;
- }
- }
-
- return cmdHandled;
- }
-
-
- // ---------------------------------------------------------------------------
- // • FindCommandStatus [public, virtual]
- // ---------------------------------------------------------------------------
- // Determine the status of a Command for the purposes of menu updating.
-
- void
- CBasicApp::FindCommandStatus(
- CommandT inCommand,
- Boolean& outEnabled,
- Boolean& outUsesMark,
- UInt16& outMark,
- Str255 outName)
- {
- switch (inCommand) {
-
- case cmd_New: {
- break;
- }
-
- default: {
- LApplication::FindCommandStatus(inCommand, outEnabled,
- outUsesMark, outMark, outName);
- break;
- }
- }
- }
-
-
- // ---------------------------------------------------------------------------
- // • RegisterClasses [protected]
- // ---------------------------------------------------------------------------
- // To reduce clutter within the Application object's constructor, class
- // registrations appear here in this seperate function for ease of use.
-
- void
- CBasicApp::RegisterClasses()
- {
- RegisterClass_(LWindow);
- RegisterClass_(LCaption);
- RegisterClass_(LPicture);
- }
-
- void
- CBasicApp::SpendTime(const EventRecord& /*inMacEvent */)
- {
- if (TickCount() >= activeTime)
- {
- if (theWindow == NULL)
- {
- ShowMyWindow();
- activeTime = TickCount() + 300;
- if ((Random()%5) == 1)
- SteveIsUpset();
- }
- else
- {
- DeleteMyWindow();
- SetActivateTime();
- }
- }
- }
-
- void
- CBasicApp::SetActivateTime()
- {
- activeTime = TickCount() + 10 + (abs(Random())%35)*60;
- }
-
- void
- CBasicApp::EventSuspend(const EventRecord& inMacEvent)
- {
- if (theWindow != NULL)
- {
- StopIdling();
- DeleteMyWindow();
- SetActivateTime();
- StartIdling();
- }
- LApplication::EventSuspend(inMacEvent);
- }
-
- void
- CBasicApp::SteveIsUpset()
- {
-
- AEDesc finderAddr;
- AppleEvent mySleep, reply;
- OSErr err;
-
- while (SpeechBusy()) {/* if steve is speaking, wait for him to finish */}
-
- err = AECreateDesc((unsigned long)typeApplSignature, "MACS", 4L, &finderAddr);
- err = AECreateAppleEvent('fndr', 'slep', &finderAddr, kAutoGenerateReturnID,
- kAnyTransactionID, &mySleep);
- err = AESend(&mySleep, &reply, kAENoReply + kAECanSwitchLayer + kAEAlwaysInteract,
- kAENormalPriority, kAEDefaultTimeout, NULL, NULL);
- AEDisposeDesc(&finderAddr);
- AEDisposeDesc(&mySleep);
- AEDisposeDesc(&reply);
-
- }
-